<?php

/**
 * Permissions Controller.
 *
 * Helpful methods to make Controller.
 *
 * PHP versions 5
 * CAKEPHP versions 2.x
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       User.Controller
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('UserAppController', 'User.Controller');

/**
 * Autocomple in PHP's IDE solution
 * 
 * @property User $User
 * @property Aco $Aco
 * @property InstallerComponent $Installer
 * 
 */
Class PermissionsController extends UserAppController {

    public $components = array(
        'Installer'
    );

    /**
     * Use model class
     *
     * @var array
     */
    public $uses = array('User.User', 'Aco', 'Aro');

    /**
     * Management permissions
     *
     * @return void
     */
    public function admin_index() {
        $aco = $this->Aco->find('first', array('recursive' => -1,
            'fields' => array('lft', 'rght'),
            'conditions' => array('parent_id' => null, 'alias' => $this->Installer->actionPath)));

        if (empty($aco)) {
            $this->redirect(array('action' => 'install'));
        }

        $this->Dashboard->toolbar(__d('system', 'Blocks management'), array(
            'save' => array('action' => 'save', 'text' => __d('system', 'Save')),
          //  'role' => array('action' => 'add', 'text' => __d('system', 'Manage roles')),
         //   'cancel' => __d('system', 'Back'),
        ));
        if ($this->request->is('post')) {
            $data = array_merge(array('task' => '', 'id' => array(0)), $this->request->data['Paginate']);
            switch (strtolower($data['task'])) {
                case 'cancel' : {
                        $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                    }
                case 'role': {
                        $this->Dashboard->redirect(array('controller' => 'roles', 'ext' => false));
                    }
                case 'save': {
                        if (!empty($this->request->data['Permission'])) {
                            foreach ($this->request->data['Permission'] as $data) {
                                foreach ($data['aro'] as $aro) {
                                    $this->Installer->permissionChange($data['aco_id'], 'Role', $aro['role'], $aro['set']);
                                }
                            }
                            $this->setFlash(__d('system', 'The %s setting has been saved', __d('system', 'Block')), 'success');
                        }
                    }
            }
            $this->redirect($this->request->here(false));
        } else {
            $acos = $this->Aco->find('all', array('recursive' => -1,
                'fields' => array('id', 'model', 'alias', 'parent_id'),
                'order' => 'rght ASC',
                'conditions' => array('lft >' => $aco['Aco']['lft'], 'rght <' => $aco['Aco']['rght'])));
            $roles = $this->User->Role->find('list');
            $administrator = $this->User->roleMap('administrator');
            $aro = $this->Aro->find('list', array(
                'fields' => array('id', 'foreign_key'),
                'recursive' => -1,
                'conditions' => array(
                    'model' => 'Role')));
            $_permission = $this->Aco->Permission->find('all', array(
                'conditions' => array(
                    'aro_id' => array_keys($aro),
                    'aco_id' => Set::classicExtract($acos, '{n}.Aco.id'),
                ),
                'fields' => array(
                    'aro_id',
                    '_update',
                    '_create',
                    '_read',
                    '_delete',
                    'aco_id'
                ),
                'recursive' => -1));
            $permission = array();
            foreach ($_permission as $_node) {
                $permission[$_node['Permission']['aco_id']][$aro[$_node['Permission']['aro_id']]] = $_node['Permission']['_delete'] == 1 &&
                        $_node['Permission']['_create'] == 1 && $_node['Permission']['_update'] == 1 && $_node['Permission']['_read'] == 1;
            }
            $acos = Set::nest($acos, array('idPath' => '/Aco/id', 'parentPath' => '/Aco/parent_id'));
            $this->set(compact('acos', 'aco', 'roles', 'permission', 'administrator'));
        }
    }

    public function admin_install() {
        $key = 'UserInstaller.Permission';
        if (!$this->Session->check($key)) {
            $this->Session->write($key, array('current' => -1, 'plugins' => CakePlugin::loaded()));
        }
        $log = array();
        extract($this->Session->read($key));
        if ($current === -1) {
            $log = $this->Installer->permission();
        } else {
            if (isset($plugins[$current])) {
                $log = $this->Installer->permission($plugins[$current]);
            }
        }
        $current++;
        $this->Session->write($key, compact('current', 'plugins'));
        if ($current >= count($plugins)) {
            $this->Session->delete($key);
            $this->setFlash(__d('system', 'The %s map has been installed', __d('system', 'Permission')), 'success');
            $this->redirect(array('action' => 'index', 'ext' => false));
        }
        $this->set(compact('log', 'current', 'plugins'));
    }

}

?>
